package com.youhong.teethcare.utils;

import android.Manifest;
import android.app.Activity;
import android.os.Build;

import com.tbruyelle.rxpermissions2.RxPermissions;


/**
 * 权限申请帮助类
 */

public class PermissionsUtils {
    //私有属性
    public interface PermissionsUtilsListener {
        void onSuccess();

        void onfail();
    }


    protected static final String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.FOREGROUND_SERVICE,
            Manifest.permission.SYSTEM_ALERT_WINDOW,
            Manifest.permission.VIBRATE,
            Manifest.permission.REORDER_TASKS,
            Manifest.permission.WAKE_LOCK,
            Manifest.permission.ACCESS_BACKGROUND_LOCATION,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.INTERNET};


    //网络状态，兼容android 11
    public static void checkNETWORK_STATE(Activity activity, PermissionsUtilsListener permissionsUtilsListener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            RxPermissions rxPermissions = new RxPermissions(activity);
            rxPermissions.request(
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.INTERNET).subscribe(aBoolean -> {
                if (aBoolean) {
                    permissionsUtilsListener.onSuccess();
                } else {
                    //只要有一个权限被拒绝，就会执行
                    permissionsUtilsListener.onfail();
                }
            });
        } else {
            permissionsUtilsListener.onSuccess();
        }
    }

    public static void checkBODY_SENSORS(Activity activity, PermissionsUtilsListener permissionsUtilsListener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            RxPermissions rxPermissions = new RxPermissions(activity);
            rxPermissions.request(
                    Manifest.permission.BODY_SENSORS).subscribe(aBoolean -> {
                if (aBoolean) {
                    permissionsUtilsListener.onSuccess();
                } else {
                    //只要有一个权限被拒绝，就会执行
                    permissionsUtilsListener.onfail();
                }
            });
        } else {
            permissionsUtilsListener.onSuccess();
        }


    }

    /**
     * 相机，读写
     */
    public static void getPermission_CAMERA(Activity activity, PermissionsUtilsListener permissionsUtilsListener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            RxPermissions rxPermissions = new RxPermissions(activity);
            rxPermissions.request(
                    Manifest.permission.CAMERA,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.INTERNET).subscribe(aBoolean -> {
                if (aBoolean) {
                    permissionsUtilsListener.onSuccess();
                } else {
                    //只要有一个权限被拒绝，就会执行
                    permissionsUtilsListener.onfail();
                }
            });
        } else {
            permissionsUtilsListener.onSuccess();
        }
    }


    /**
     * 定位蓝牙权限等
     */
    public static void getPermission_Location(Activity activity, PermissionsUtilsListener permissionsUtilsListener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            RxPermissions rxPermissions = new RxPermissions(activity);
            rxPermissions.request(
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.BLUETOOTH,
                    Manifest.permission.BLUETOOTH_ADMIN,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.INTERNET).subscribe(aBoolean -> {
                if (aBoolean) {
                    permissionsUtilsListener.onSuccess();
                } else {
                    //只要有一个权限被拒绝，就会执行
                    permissionsUtilsListener.onfail();
                }
            });
        } else {
            permissionsUtilsListener.onSuccess();
        }
    }

    /**
     * 定位蓝牙权限等
     */
    public static void getPermission_WAKE_LOCK(Activity activity, PermissionsUtilsListener permissionsUtilsListener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            RxPermissions rxPermissions = new RxPermissions(activity);
            rxPermissions.request(
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                    Manifest.permission.BLUETOOTH,
                    Manifest.permission.BLUETOOTH_ADMIN,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.INTERNET).subscribe(aBoolean -> {
                if (aBoolean) {
                    permissionsUtilsListener.onSuccess();
                } else {
                    //只要有一个权限被拒绝，就会执行
                    permissionsUtilsListener.onfail();
                }
            });
        } else {
            permissionsUtilsListener.onSuccess();
        }
    }


    /**
     * 电池电量白名单
     *
     * @param activity
     * @param permissionsUtilsListener
     */
    public static void getPermission_BATTERY_OPTIMIZATIONS(Activity activity, PermissionsUtilsListener permissionsUtilsListener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            RxPermissions rxPermissions = new RxPermissions(activity);
            rxPermissions.request(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
            ).subscribe(aBoolean -> {
                if (aBoolean) {
                    permissionsUtilsListener.onSuccess();
                } else {
                    //只要有一个权限被拒绝，就会执行
                    permissionsUtilsListener.onfail();
                }
            });
        } else {
            permissionsUtilsListener.onSuccess();
        }
    }


}
